<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        ul li {
            list-style: none;
            width: 50px;
            height: 50px;
            margin-bottom: 10px;
            display: inline-block;
            text-align: center;
        }

        .cenient {
            width: 500px;
            height: 100px;
            margin: 200px auto;
        }

        ul {
            /* display: flex; */
            width: 100%;
            /* justify-content: space-between; */
            /* text-align: center; */
            /* align-items: center; */
            height: 50px;
            position: relative;

        }


        a {
            text-decoration: none;
            width: 50px;
            height: 50px;
            color: black;
            display: inline-block;
        }

        .hengxian {
            width: 50px;
            height: 3px;
            background-color: red;
            position: absolute;
            bottom: 10px;
            transition: all .3s ease-out;
            /* transform: translatex(); */
            /* left: 115px; */
        }
    </style>
</head>

<body>
    <div class="cenient">
        <div class="box">
            <ul>
                <li class="list"><a href="#">首页</a></li>
                <li class="list"><a href="#">视频</a></li>
                <li class="list"><a href="#">时尚</a></li>
                <li class="list"><a href="#">音乐</a></li>
                <li class="list"><a href="#">游戏</a></li>
                <div class="hengxian"></div>
            </ul>
        </div>
    </div>
    <script>
        const a = document.querySelectorAll('.list a')
        const hengxian = document.querySelector('.hengxian')
        for (let i = 0; i < a.length; i++) {
            a[i].addEventListener('click', function (e) {
                // e.target.tagName 获取到了a标签
                if (e.target.tagName === 'A') {
                    // console.log(e.target.offsetLeft);
                    // e.target.offsetLeft 获取到了a标签对于父盒子左边的距离 hengxian的盒子必须是定位元素才有效
                    hengxian.style.transform = `translatex(${e.target.offsetLeft}px)`
                }
            })

        }
    </script>
</body>

</html>